package com.ruoyi.wxlogin.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.wxauthentication.pojo.StudentInfo;
import com.ruoyi.wxlogin.pojo.WxMpUser;
import com.ruoyi.wxlogin.pojo.WxUser;
import org.apache.commons.lang.StringUtils;
import com.ruoyi.wxcommon.common.Result;
import com.ruoyi.wxcommon.constant.RedisKey;
import com.ruoyi.wxlogin.feign.AreaFeignService;
import com.ruoyi.wxlogin.feign.AuthenticationFeignService;
import com.ruoyi.wxlogin.mapper.WxMpUserMapper;
import com.ruoyi.wxlogin.mapper.WxUserMapper;
import com.ruoyi.wxlogin.service.IWxUserLoginService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.concurrent.TimeUnit;

import static com.ruoyi.wxlogin.utils.RedisM.RedisInJava;

@Service
public class IWxUserLoginServiceImpl extends ServiceImpl<WxUserMapper, WxUser> implements IWxUserLoginService {
    @Autowired
    WxMpUserMapper wxMpUserMapper;
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    AreaFeignService areaFeignService;
    @Autowired
    AuthenticationFeignService authenticationFeignService;
//    @Autowired
//    ImageUtils imageUtils;
    @Transactional
    @Override
    public Result login(WxUser wxUser) {
        String result = redisTemplate.opsForValue().get(RedisKey.WX_USER + wxUser.getOpenId() + RedisKey.USERINFO);
        if (StringUtils.isNotEmpty(result) && wxUser.getOpenId() != null){
            WxUser user = RedisInJava(result, WxUser.class);
            return Result.SUCCESS(user);
        }
        if (wxUser.getId() == null && wxUser.getOpenId() != null){
            WxUser user = this.baseMapper.byOpenId(wxUser.getOpenId());
            if (user != null){
                String json = JSON.toJSONString(user);
                redisTemplate.opsForValue().set(RedisKey.WX_USER + wxUser.getOpenId() + RedisKey.USERINFO, json,7, TimeUnit.DAYS);
                return Result.SUCCESS(user);
            }
            wxUser.setCreateTime(new Timestamp(System.currentTimeMillis()));
            this.baseMapper.insert(wxUser);
            String json = JSON.toJSONString(wxUser);
            redisTemplate.opsForValue().set(RedisKey.WX_USER + wxUser.getOpenId() + RedisKey.USERINFO, json,7, TimeUnit.DAYS);
            WxUser newUser = this.baseMapper.byOpenId(wxUser.getOpenId());
            WxMpUser wxMpUser = new WxMpUser();
            BeanUtils.copyProperties(newUser,wxMpUser);
            wxMpUser.setWxId(newUser.getId());
            wxMpUser.setState(1);
            wxMpUserMapper.insert(wxMpUser);
            return Result.SUCCESS(newUser);
        }else {
            return Result.FAIL("登录失败");
        }
    }
    @Transactional
    @Override
    public Result wxUserInfo(WxUser wxUser) {
        this.baseMapper.updateById(wxUser);
        WxUser user = this.baseMapper.selectById(wxUser.getId());
        String json = JSON.toJSONString(user);
        redisTemplate.opsForValue().set(RedisKey.WX_USER + user.getOpenId() + RedisKey.USERINFO, json,7, TimeUnit.DAYS);
        return Result.SUCCESS(user);
    }

    @Override
    public Result updateWxUser(WxUser wxUser) {
        updateUserInfo(wxUser);
        return Result.SUCCESS("修改成功");
    }
    @Transactional
    @Override
    public Result updateWxUserAuthentication(Integer id,String openId,Long studentNumber,String identificationNumber) {

        Result<StudentInfo> result = authenticationFeignService.authentication(studentNumber, identificationNumber);
        if (result.getData() != null){
            StudentInfo studentInfo = result.getData();
            WxUser wxUser = new WxUser();
            wxUser.setId(id);
            wxUser.setDphone(String.valueOf(studentNumber));
            wxUser.setAId(studentInfo.getAId());
            wxUser.setName(studentInfo.getName());
            wxUser.setOpenId(openId);
            updateUserInfo(wxUser);
            return Result.SUCCESS(this.baseMapper.selectById(id));
        }
        return Result.FAIL("认证失败,信息不匹配");
    }

    private void updateUserInfo(WxUser wxUser) {
        QueryWrapper<WxUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",wxUser.getId());
        this.baseMapper.update(wxUser,queryWrapper);

        String result = redisTemplate.opsForValue().get(RedisKey.WX_USER + wxUser.getOpenId() + RedisKey.USERINFO);
        if (StringUtils.isNotEmpty(result)){
            String json = JSON.toJSONString(this.baseMapper.selectById(wxUser.getId()));
            redisTemplate.opsForValue().set(RedisKey.WX_USER + wxUser.getOpenId() + RedisKey.USERINFO, json,7, TimeUnit.DAYS);
        }
    }

    @Override
    public Result getAddress(Integer defaultAddress) {
        if (areaFeignService.userInfo(defaultAddress) != null){
            return Result.SUCCESS(areaFeignService.userInfo(defaultAddress));
        }else {
            return Result.FAIL("暂无默认地址");
        }

    }




}
